package com.jshare.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.jshare.entity.Member;
import com.jshare.service.MemberService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class MemberAction extends ActionSupport{

	@Resource(name="memberService")
	private MemberService memberService;
	private String message;
	private Member member;
	private List<Member> members;
	public String execute()
	{
		return "memberCenter";
	}
	public String register()
	{
		return "register";
	}
	public String login()
	{
		return "login";
	}
	public String loginDeal() throws Exception
	{
		if(memberService.login(member)== true)
		{
			ActionContext atc = ActionContext.getContext();
			List loadMembers = memberService.loadMemberByExample(member);
			Member loadMember = (Member) loadMembers.get(0);
			if(loadMember.getRole().equals("admin"))
			{
				atc.getSession().put("admin", member);
				return "adminPage";
			}else
			{
				atc.getSession().put("member", member);
				return "RememberCenter";
			}
		}else
		{
			return "login";
		}
		
	}
	public String registerDeal()
	{
			member.setRegtime(new Timestamp(new Date().getTime()));
			member.setRole("simple");
			memberService.addMember(member);
			return "regSuccess";
	}
	public void validateRegisterDeal()
	{
		ActionContext atx = ActionContext.getContext();
		if(member.getName().length() <6 || member.getName().length() > 15)
		{
			addFieldError("member.name", "用户名的长度大小为6-15");
		}else
		{
			Member loadMember = memberService.loadMember(member.getName());
			if(loadMember != null)
			{
				addFieldError("member.name", "用户名已存在");
			}
		}
		if(member.getPasswd().length()<6)
		{
			addFieldError("member.passwd", "密码不能小于6位");
		}
		Map<String, Object> params = atx.getParameters();
		String[] passwd2 = (String[]) params.get("passwd2");
		if(!member.getPasswd().equals(passwd2[0]))
		{
			addFieldError("passwd2", "密码不一致");
		}
		if(member.getEmail().length()<5)
		{
			addFieldError("member.email", "邮箱无效");
		}else
		{
			 String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			 Pattern regex = Pattern.compile(check);
			 Matcher matcher = regex.matcher(member.getEmail());
			 if(matcher.matches() == false)
			 {
				 addFieldError("member.email", "邮箱格式不正确");
			 }
		}
	}
	public String logoff()
	{
		HttpServletRequest request=ServletActionContext.getRequest();   
        
        HttpSession session=request.getSession();
		if(session!=null && session.getAttribute("admin")!=null)
		{
			session.removeAttribute("admin");
		}else if(session!=null && session.getAttribute("member")!=null)
		{
			session.removeAttribute("member");
		}
		return "homePage";
	}
	public String memberCenter()
	{
		HttpServletRequest request= ServletActionContext.getRequest();
		String module= request.getParameter("module");
		String result="memberCenter";
		if(module!=null && module.length() > 1)
		{	
		if(module.equals("article"))
		{
			result = "memberArticle";
		}else if(module.equals("tiezi"))
		{
			result = "memberTiezi";
		}else if(module.equals("myinfo"))
		{
			result = "memberMyinfo";
		}
		}
		member = memberService.loadMember(member.getName());
		return result;
	}
	public String memberList()
	{
		members = memberService.loadAll();
		return "memberList";
	}
	public String modify()
	{
		HttpServletRequest request= ServletActionContext.getRequest();
		String id=request.getParameter("id");
		member = memberService.loadMember(id);
		return "modify";
	}
	public String delete()
	{
		ActionContext atx = ActionContext.getContext();
		Map Parameters = atx.getParameters();
		String[] ids = (String[]) Parameters.get("id");
		String id = ids[0];
		System.out.println(id);
		if(id!=null && !id.equals(""))
		{
			memberService.deleteMember(id);
		}
		members = memberService.loadAll();
		return "memberList";
	}
	public MemberService getMemberService() {                     
			return memberService;                                     
    }                                                                 public void setMemberService(MemberService memberService) {       	this.memberService = memberService;                           }                                                                 public String getMessage() {                                      	return message;                                               }                                                                 public void setMessage(String message) {                          	this.message = message;                                       }

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}
	public List<Member> getMembers() {
		return members;
	}
	public void setMembers(List<Member> members) {
		this.members = members;
	}

	
}
    